package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JTextArea;
import javax.swing.event.ChangeListener;

import ViewPropertyPanel.DrawModifierButtons;

import Actions.ActionFactory;

import drawPropertyCommands.DrawProperties;
import drawPropertyCommands.GraphicsProperties;
import drawPropertyCommands.IDrawModifier;

import tabbedCanvas.LayeredCanvas;

public class ViewController implements IViewController , ActionListener {
	View window;
	List<LayeredCanvas> panelList;
	JTextArea modelFeedback;
	GraphicsProperties activeGraphicsProperties;
	Map<Integer, GraphicsProperties> propertiesMap = new HashMap<Integer, GraphicsProperties>();
	List<Integer> tabList = new ArrayList<Integer>();
	Integer activeTab=null;
	ScriptEditor child;
	
	public ViewController(ActionListener ActionController, ChangeListener ChangeController){
		window = new View(ActionController,ChangeController, this);
		panelList =  window.getCanvasList();
		modelFeedback = window.getTextArea();
		activeGraphicsProperties = new GraphicsProperties();
		propertiesMap.put(0,activeGraphicsProperties );
		ActionFactory.loadActions();
	}

	/**
	 * draw turtle with id ID on tab with tabID to from (x1, y1) to (x2, y2) and turn degrees.
	 * boolean trail determines of trail is left behind the move.
	 */
	@Override
	public void draw(int x1, int y1, int x2, int y2, int degrees,
			boolean trail, int ID, int tabID) {
		panelList.get(tabID).move(x1, y1, x2, y2, degrees, trail, ID, (DrawProperties)activeGraphicsProperties.clone());
		
	}


	/**
	 * display text on the view
	 */
	@Override
	public void displayText(String s) {
		modelFeedback.append(">  " + s + "\n");
		window.scrollPaneToBottom();
	}

	@Override
	public void updateHistory(String msg) {
		window.addHistory(msg);
	}

	@Override
	public void updateVariable(String variable) {
		window.addVariable(variable);
		
	}

	/**
	 * add new Turtle
	 */
	@Override
	public void addTurtle(int turtleID, int tabID) {
		panelList.get(tabID).addTurtle(turtleID);
	}

	@Override
	public void clearTab(int tabId) {
		// TODO implement a cleartab! 
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// Change Pen Properties
		if((e.getSource()) instanceof DrawModifierButtons){
			((DrawModifierButtons)e.getSource()).executeModify(activeGraphicsProperties);
		} else {
		// Menu Item
			ActionFactory.createAction(e.getActionCommand().replaceAll(" ", ""), this, e);
		}
	}
    
    public void initializeTab(int ID){
        tabList.add(ID);
        propertiesMap.put(ID, new GraphicsProperties());
    }
    
    public void setActiveTab(int ID){
        if(!tabList.contains(ID)){
            initializeTab(ID);
        }
        activeTab=ID;
        activeGraphicsProperties = propertiesMap.get(ID);
    }

    @Override
    public void setDisplayProperties(IDisplayProperties dp, int ID) {
        // TODO Auto-generated method stub
        
    }

	@Override
	public void cleanAndUpdate(List<String> list) {
		window.cleanAndUpdate(list);
		
	}
	
	/**
	 * Allows View controller to manage actions for the script editor
	 * @param child ScriptEditor object currently being used
	 */
	public void setChildFrame (ScriptEditor child)
	{
		this.child = child;
	}
	
	/**
	 * Allows ViewController to access text in the script editor
	 * @return ScriptEditor object
	 */
	public ScriptEditor getChildFrame(){
		return child;
	}

    
}
